<template>
  <el-card>
    <span slot="header">账号列表</span>
    <el-table ref="tableRef" :data="tableData"
      @selection-change="handleSelectionChange">
      <!-- 多选框 -->
      <el-table-column type="selection" width="55"></el-table-column>
      <!-- 账号 -->
      <el-table-column prop="account" label="账号"></el-table-column>
      <!-- 用户组 -->
      <el-table-column prop="userGroup" label="用户组" show-overflow-tooltip>
      </el-table-column>
      <!-- 创建时间 -->
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          {{ scope.row.ctime | formatYMD }}
        </template>
      </el-table-column>

      <!-- 操作 -->
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" style="margin-right: 10px"
            @click="handleEdit(scope.$index, scope.row)">
            编辑
          </el-button>
          <!-- 气泡确认框 -->
          <el-popconfirm title="这是一段内容确定删除吗？"
            @confirm="handleDelete(scope.row)">
            <el-button slot="reference" size="mini" type="danger">
              删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination class="mt20" background @size-change="handleSizeChange"
      @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="[3, 5]" :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <!-- 操作 -->
    <div class="mt20">
      <el-button type="danger" @click="handleBatchDel">批量删除</el-button>
      <el-button type="primary" @click="cancelChange">取消选择</el-button>
    </div>

    <!-- 编辑弹窗 -->
    <el-dialog title="编辑账号" :visible.sync="dialogFormVisible" class="dialog">
      <el-form :model="formData">
        <el-form-item label="账号"  label-width="55px" class="input">
          <el-input v-model="formData.account" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户组">
          <el-select v-model="formData.userGroup" placeholder="请选择用户组">
            <el-option label="普通用户" value="普通用户"></el-option>
            <el-option label="超级管理员" value="超级管理员"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  getUsersListReq,
  delUsersReq,
  editUserReq,
  batchdelUserReq,
} from "@/api/user";

export default {
  data() {
    return {
      tableData: [],
      //多选数据内容id放在一个数组ids里
      ids: [],
      currentPage: 1,
      pageSize: 3,
      total: 0,

      dialogFormVisible: false, //修改弹窗显示与隐藏
      //弹窗数据
      formData: {
        id: "",
        account: "",
        userGroup: "",
      },
    };
  },
  methods: {
    //获取数据函数
    async getData() {
      //提取数据
      let currentPage = this.currentPage;
      let pageSize = this.pageSize;
      //发送请求
      let res = await getUsersListReq({ currentPage, pageSize });

      //解构数据
      let { data, total } = res.data;

      //数据赋值
      this.total = total;
      this.tableData = data;

      //解决删除BUG
      if (data.length === 0 && this.tableData !== 1) {
        this.currentPage--;
        this.getData();
      }
    },

    //checkbox 选择时候触发
    handleSelectionChange(val) {
      this.ids = val.map((v) => v.id);
      // console.log('this.ids :>> ', this.ids);
    },
    //批量删除
    handleBatchDel() {
      // console.log('this.ids :>> ', this.ids);
      if (this.ids.length !== 0) {
        this.$confirm("此操作将永久删除选中用户, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(async () => {
            //发请求
            let res = await batchdelUserReq({ ids: this.ids });
            let { code, msg } = res.data;
            if (code === 0) {
              // this.$message({
              //   type: 'success',
              //   message: '删除成功!',
              // })
              this.getData();
              this.$message.success("删除成功");
            }
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      } else {
        this.$message.info("亲，你还没有选中任何数据哦！");
      }
    },

    //取消选择
    cancelChange() {
      if (this.ids.length) {
        this.$refs.tableRef.clearSelection();
        this.$message.info("已取消删除");
      } else {
        this.$message.info("亲，你还没有选中任何数据哦！");
      }
    },

    //编辑----数据回填
    async handleEdit(index, row) {
      console.log(index, row);
      //打开弹窗
      this.dialogFormVisible = true;
      //获取当前数据
      this.formData = { ...row };
    },
    //确认编辑
    async handleConfirm() {
      //发送请求
      let res = await editUserReq(this.formData);
      //结构数据
      let { code, msg } = res.data;
      //判断
      if (code === 0) {
        this.dialogFormVisible = false;
        this.getData();
      }
    },

    //删除
    async handleDelete(row) {
      // console.log(row)
      //获取id
      let { id } = row;
      let res = await delUsersReq({ id });
      let { code, msg } = res.data;
      //删除后  重新拉取数据
      if (code === 0) {
        this.getData();
      }
    },
    // 改变每页条数
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pageSize = val;
      this.getData();
    },
    //改变当前页
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      //1.拿到当前页   赋值
      this.currentPage = val;
      //2.发请求
      this.getData();
    },
  },

  //created生命周期  初始化发送请求
  created() {
    this.getData();
  },
};
</script>

<style lang="scss" scoped>
// form {
//   width: 300px;
// }
.input {
  :deep(.el-input__inner) {
    width: 250px;
  }
}
</style>
